﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using Aden.Model.SQLReport;
using Aden.Util.Database;
using Aden.Util.Reports;
using System.IO;

namespace Aden.BLL.Reports
{
    public class ReportHelper
    {
        public static void ShowPDF(string reportName, string data)
        {
            try
            {
                Dictionary<string, string> keyDic =
                    Aden.Util.Common.JsonHelper.JSDeserialize<Dictionary<string, string>>(data);

                if (string.IsNullOrWhiteSpace(reportName))
                    throw new Exception("请明确报表名");

                string sql = "select * from tblcrystalreports (nolock) where reportname = '{0}' ";
                sql = string.Format(sql, reportName);

                DataTable dt = SqlServerHelper.GetDataTable(SqlServerHelper.salesorderConn(), sql);

                if (dt == null || dt.Rows.Count == 0) 
                    throw new Exception(string.Format("报表 ({0}) 的配置信息", reportName));

                sql = dt.AsEnumerable().FirstOrDefault().Field<string>("datasourcesql").ToString();

                if (string.IsNullOrWhiteSpace(sql))
                {
                    (new Aden.Util.Reports.CrystalReports(dt.Rows[0]["ServerIP"].ToString(), dt.Rows[0]["DatabaseName"].ToString(),
                           dt.Rows[0]["UserName"].ToString(), dt.Rows[0]["Password"].ToString()))
                           .ExportPDF(dt.Rows[0]["FileName"].ToString(), keyDic);
                }
                else
                {
                    if (!keyDic.ContainsKey("PODate") || !keyDic.ContainsKey("SupplierCode"))
                        throw new Exception(string.Format("报表 ({0}) 的配置信息", reportName));
                    sql = string.Format(sql, keyDic["PODate"], keyDic["SupplierCode"]);
                    DataTable ds = SqlServerHelper.GetDataTable(SqlServerHelper.salesorderConn(), sql);
                    if (ds == null || ds.Rows.Count == 0)
                        throw new Exception("无数据");
                    List<Dictionary<string, string>> keyDicList = new List<Dictionary<string, string>>();
                    foreach (DataRow row in ds.AsEnumerable())
                    {
                        keyDic = new Dictionary<string, string>();

                        keyDic.Add("Creditor", row.Field<string>("SupplierCode"));
                        keyDic.Add("PODate", row.Field<string>("PODate"));
                        // keyDic.Add("Division", row.Field<string>("Division"));
                    }

                    (new Aden.Util.Reports.CrystalReports(dt.Rows[0]["ServerIP"].ToString(), dt.Rows[0]["DatabaseName"].ToString(),
                            dt.Rows[0]["UserName"].ToString(), dt.Rows[0]["Password"].ToString()))
                            .ExportPDF(dt.Rows[0]["FileName"].ToString(), keyDic);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static void ShowWord(string reportName, string data)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(reportName))
                    throw new Exception("请明确报表名");

                Dictionary<string, string> keyDic =
                    Aden.Util.Common.JsonHelper.JSDeserialize<Dictionary<string, string>>(data);

                string sql = "select * from tblcrystalreports where reportname='{0}'";
                sql = string.Format(sql, reportName);
                DataTable dt = SqlServerHelper.GetDataTable(SqlServerHelper.salesorderConn(), sql);
                if (dt == null || dt.Rows.Count == 0) throw new Exception(string.Format("报表 ({0}) 的配置信息", reportName));

                (new Aden.Util.Reports.CrystalReports(dt.Rows[0]["ServerIP"].ToString(), dt.Rows[0]["DatabaseName"].ToString(),
                    dt.Rows[0]["UserName"].ToString(), dt.Rows[0]["Password"].ToString()))
                    .ExportWord(dt.Rows[0]["FileName"].ToString(), keyDic);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static void ShowExcel(string data)
        {
            try
            {
                Dictionary<string, string> keyDic =
                    Aden.Util.Common.JsonHelper.JSDeserialize<Dictionary<string, string>>(data);

                if (keyDic["reportType"] == "weeklyMenu")
                    new Aden.Util.Reports.ExcelReport().DownloadExcelFile
                        (Aden.DAL.ReportLib.DownloadReport.
                        ExportExcelWeeklyMenu(keyDic));
                else if (keyDic["reportType"] == "siteWeeklyMenu")
                    new Aden.Util.Reports.ExcelReport().DownloadExcelFile
                        (Aden.DAL.ReportLib.DownloadReport.
                        ExportSiteExcelWeeklyMenu(keyDic));
                else if (keyDic["reportType"] == "menuProcess")
                    new Aden.Util.Reports.ExcelReport().DownloadExcelFile
                        (Aden.DAL.ReportLib.DownloadReport.
                        ExportExcelMenuProcess(keyDic));
                else if (keyDic["reportType"] == "wxRechargeReport")
                    new Aden.Util.Reports.ExcelReport().DownloadExcelFile
                        (Aden.DAL.ReportLib.DownloadReport.
                        WechatRechargeReport(keyDic));
                else if (keyDic["reportType"] == "mailweeklyMenu")
                    new Aden.Util.Reports.MailHelper().SendMail(keyDic["sendType"], keyDic["recipients"], (Aden.DAL.ReportLib.DownloadReport.
                    ExportExcelWeeklyMenu(keyDic)));
                else if (keyDic["reportType"] == "SiteSurveyReport")
                    new Aden.Util.Reports.ExcelReport().DownloadExcelFile
                        (Aden.DAL.ReportLib.DownloadReport.
                        SiteSurveyReport(keyDic));
                else if (keyDic["reportType"] == "InvalidItemList")
                    new Aden.Util.Reports.ExcelReport().DownloadExcelFile
                       (Aden.DAL.ReportLib.DownloadReport.
                       InvalidPurchaseItems(keyDic));
                else if (keyDic["reportType"] == "customizedReport")
                {
                    new Aden.Util.Reports.ExcelReport().DownloadExcelFile
                      (Aden.DAL.ReportLib.DownloadReport.
                      GetCustomizedReport(keyDic));
                }
                else if (keyDic["reportType"] == "VNTempRpt")
                {
                    new Aden.Util.Reports.ExcelReport().DownloadExcelFile
                        (Aden.DAL.ReportLib.DownloadReport.
                        GetVNPOTempt(keyDic));
                } 
                else if(keyDic["reportType"] == "GCNMIC")
                {
                    ShowWord("GCNMIC", data);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 报告参数显示列表
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static List<SqlReportParam> ParamControl (string data)
        {
            try
            {
                Dictionary<string, string> keyDic =
                        Aden.Util.Common.JsonHelper.JSDeserialize<Dictionary<string, string>>(data);
                List<SqlReportParam> list = Aden.DAL.ReportLib.DownloadReport.ParamControl(keyDic);
                return list;
            }
            catch(Exception ex)
            {
                throw ex;
            }

        }

    }
}
